/*
 * Created on 2005-3-1
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author sunan
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class DateUtils
{

	static Calendar cld;

	public static final long YEAR = 10000000000l;

	public static final long MONTH = 100000000;

	public static final long DAY = 1000000;

	public static final long HOUR = 10000;

	public static final long MINUTE = 100;

	public static long newDate()
	{
		long date = 0;
		return date;
	}

	public static long setYear(long date, long year)
	{
		date = year * YEAR + date % YEAR;
		return date;
	}

	public static long setYear(long date, String year)
	{
		long thisYear = 0;
		try
		{
			thisYear = Long.parseLong(year);
		} catch (Exception e)
		{
			return date;
		}

		return setYear(date, thisYear);
	}

	public static long getYear(long date)
	{

		return date / YEAR;
	}

	public static long setMonth(long date, long month)
	{
		long year = getYear(date);
		date = date - year * YEAR;
		date = month * MONTH + date % MONTH;

		return year * YEAR + date;
	}

	public static long setMonth(long date, String month)
	{
		long thisMonth = 0;
		try
		{
			thisMonth = Long.parseLong(month);
		} catch (Exception e)
		{
			return date;
		}

		return setMonth(date, thisMonth);
	}

	public static long getMonth(long date)
	{
		long year = getYear(date);
		date = date - year * YEAR;

		return date / MONTH;
	}

	public static long setDay(long date, long day)
	{
		long year = getYear(date);
		long month = getMonth(date);
		date = (date - year * YEAR) - month * MONTH;
		date = day * DAY + date % DAY;

		return year * YEAR + month * MONTH + date;
	}

	public static long setDay(long date, String day)
	{
		long thisDay = 0;
		try
		{
			thisDay = Long.parseLong(day);
		} catch (Exception e)
		{
			return date;
		}

		return setDay(date, thisDay);
	}

	public static long getDay(long date)
	{
		long year = getYear(date);
		long month = getMonth(date);
		date = date - year * YEAR - month * MONTH;

		return date / DAY;
	}

	public static long setHour(long date, long hour)
	{
		long thisDate = 0;
		long year = getYear(date);
		long month = getMonth(date);
		long day = getDay(date);

		thisDate = date % HOUR;
		thisDate = year * YEAR + month * MONTH + day * DAY + hour * HOUR + thisDate;

		return thisDate;
	}

	public static long setHour(long date, String hour)
	{
		long thisHour = 0;
		try
		{
			thisHour = Long.parseLong(hour);
		} catch (Exception e)
		{
			return date;
		}

		return setHour(date, thisHour);

	}

	public static long getHour(long date)
	{
		long hour = 0;
		hour = date % DAY;
		hour = hour / HOUR;
		return hour;
	}

	public static long setMinute(long date, long minute)
	{
		long thisDate = 0;
		long year = getYear(date);
		long month = getMonth(date);
		long day = getDay(date);
		long hour = getHour(date);

		thisDate = date % MINUTE;
		thisDate = year * YEAR + month * MONTH + day * DAY + hour * HOUR + minute * MINUTE + thisDate;

		return thisDate;
	}

	public static long setMinute(long date, String minute)
	{
		long thisMinute = 0;
		try
		{
			thisMinute = Long.parseLong(minute);
		} catch (Exception e)
		{
			return date;
		}

		return setMinute(date, thisMinute);
	}

	public static long getMinute(long date)
	{
		long minute = 0;

		minute = date % HOUR;
		minute = minute / MINUTE;

		return minute;
	}

	public static long setSecond(long date, long second)
	{
		long thisDate = 0;
		long year = getYear(date);
		long month = getMonth(date);
		long day = getDay(date);
		long hour = getHour(date);
		long minute = getMinute(date);

		thisDate = year * YEAR + month * MONTH + day * DAY + hour * HOUR + minute * MINUTE + second;

		return thisDate;
	}

	public static long setSecond(long date, String second)
	{
		long thisSecond = 0;
		try
		{
			thisSecond = Long.parseLong(second);
		} catch (Exception e)
		{
			return date;
		}

		return setSecond(date, thisSecond);
	}

	public static long getSecond(long date)
	{
		long second = 0;
		second = date % MINUTE;

		return second;
	}

	public static long getNowDate()
	{
		long date = 0;
		Date nowDate = new Date();
		DateFormat df = DateFormat.getDateInstance();
		String sDate = df.format(nowDate);
		int start = 0;
		int end = 0;
		String year = null;
		String month = null;
		String day = null;
		end = sDate.indexOf("-", start);
		if (end > 0)
		{
			year = sDate.substring(start, end);
		}
		start = end + 1;
		end = sDate.indexOf("-", start);
		if (end > 0)
		{
			month = sDate.substring(start, end);
		}
		start = end + 1;
		day = sDate.substring(start);
		/**
		 * debug start
		 */
		// System.out.println("year: " + year + "/month: " + month + "/day: " +
		// day);
		/**
		 * debug end
		 */
		date = setYear(date, year);
		date = setMonth(date, Long.parseLong(month));
		date = setDay(date, day);

		return date;
	}

	public static long getNowDateTime()
	{
		long date = 0;

		Date nowDate = new Date();
		DateFormat df = DateFormat.getDateTimeInstance();
		String sDate = df.format(nowDate);
		int start = 0;
		int end = 0;
		String year = null;
		String month = null;
		String day = null;
		String hour = null;
		String minute = null;
		String second = null;
		end = sDate.indexOf("-", start);
		if (end > 0)
		{
			year = sDate.substring(start, end);
		}
		start = end + 1;
		end = sDate.indexOf("-", start);
		if (end > 0)
		{
			month = sDate.substring(start, end);
		}

		start = end + 1;
		end = sDate.indexOf(" ", start);
		if (end > 0)
		{
			day = sDate.substring(start, end);
		}

		start = end + 1;
		end = sDate.indexOf(":", start);
		if (end > 0)
		{
			hour = sDate.substring(start, end);
		}

		start = end + 1;
		end = sDate.indexOf(":", start);
		if (end > 0)
		{
			minute = sDate.substring(start, end);
		}

		start = end + 1;
		second = sDate.substring(start);

		/**
		 * debug start
		 */
		// System.out.println("year: " + year + "/month: " + month + "/day: " +
		// day + "/hour: " + hour + "/minute: " + minute + "/second: " + second
		// + "\n" + sDate);
		/**
		 * debug end
		 */

		date = setYear(date, year);
		date = setMonth(date, month);
		date = setDay(date, day);
		date = setHour(date, hour);
		date = setMinute(date, minute);
		date = setSecond(date, second);
		return date;
	}

	public static String getDate(long date, String separator)
	{
		String returnDate = null;
		String day = null;
		String month = null;
		String year = null;
		year = Long.toString(getYear(date));
		if (getMonth(date) < 10)
		{
			month = "0" + Long.toString(getMonth(date));
		} else
		{
			month = Long.toString(getMonth(date));
		}

		if (getDay(date) < 10)
		{
			day = "0" + Long.toString(getDay(date));
		} else
		{
			day = Long.toString(getDay(date));
		}
		returnDate = year + separator + month + separator + day;

		return returnDate;
	}

	public static String getDate(long date)
	{
		String returnDate = "";
		String day = null;
		String month = null;
		String year = null;
		year = Long.toString(getYear(date));
		if (getMonth(date) < 10)
		{
			month = "0" + Long.toString(getMonth(date));
		} else
		{
			month = Long.toString(getMonth(date));
		}

		if (getDay(date) < 10)
		{
			day = "0" + Long.toString(getDay(date));
		} else
		{
			day = Long.toString(getDay(date));
		}
		returnDate = year + "-" + month + "-" + day;

		return returnDate;
	}

	public static String getDateCn(long date)
	{
		String returnDate = "";
		String day = null;
		String month = null;
		String year = null;
		String hour = null;
		String minute = null;
		String second = null;

		year = Long.toString(getYear(date));
		if (getMonth(date) < 10)
		{
			month = "0" + Long.toString(getMonth(date));
		} else
		{
			month = Long.toString(getMonth(date));
		}

		if (getDay(date) < 10)
		{
			day = "0" + Long.toString(getDay(date));
		} else
		{
			day = Long.toString(getDay(date));
		}

		if (getHour(date) < 10)
		{
			hour = "0" + Long.toString(getHour(date));
		} else
		{
			hour = Long.toString(getHour(date));
		}

		if (getMinute(date) < 10)
		{
			minute = "0" + Long.toString(getMinute(date));
		} else
		{
			minute = Long.toString(getMinute(date));
		}

		if (getSecond(date) < 10)
		{
			second = "0" + Long.toString(getSecond(date));
		} else
		{
			second = Long.toString(getSecond(date));
		}

		returnDate = year + "年" + month + "月" + day + "日" + hour + "点" + minute + "分" + second + "秒";

		return returnDate;

	}

	public static long getDateTime(Date someDate)
	{
		long date = 0;

		Date nowDate = someDate;
		DateFormat df = DateFormat.getDateTimeInstance();
		String sDate = df.format(nowDate);
		int start = 0;
		int end = 0;
		String year = null;
		String month = null;
		String day = null;
		String hour = null;
		String minute = null;
		String second = null;
		end = sDate.indexOf("-", start);
		if (end > 0)
		{
			year = sDate.substring(start, end);
		}
		start = end + 1;
		end = sDate.indexOf("-", start);
		if (end > 0)
		{
			month = sDate.substring(start, end);
		}

		start = end + 1;
		end = sDate.indexOf(" ", start);
		if (end > 0)
		{
			day = sDate.substring(start, end);
		}

		start = end + 1;
		end = sDate.indexOf(":", start);
		if (end > 0)
		{
			hour = sDate.substring(start, end);
		}

		start = end + 1;
		end = sDate.indexOf(":", start);
		if (end > 0)
		{
			minute = sDate.substring(start, end);
		}

		start = end + 1;
		second = sDate.substring(start);

		/**
		 * debug start
		 */
		// System.out.println("year: " + year + "/month: " + month + "/day: " +
		// day + "/hour: " + hour + "/minute: " + minute + "/second: " + second
		// + "\n" + sDate);
		/**
		 * debug end
		 */

		date = setYear(date, year);
		date = setMonth(date, month);
		date = setDay(date, day);
		date = setHour(date, hour);
		date = setMinute(date, minute);
		date = setSecond(date, second);
		return date;
	}

	public static Long stringToLong(String someDate) throws ParseException
	{
		if(someDate==null||someDate.equals(""))
			return null;
		String format = "yyyy-MM-dd";
		if(someDate.length() > 10){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat dd=new SimpleDateFormat(format); 
		return dd.parse(someDate).getTime();
	}

	public static Date longToDate(Long param)
	{
		if (cld == null)
		{
			cld = new GregorianCalendar();
		}
		if (param != null&&param!=0)
		{
			cld.clear();
			cld.setTimeInMillis(param);
			return cld.getTime();
		} else
		{
			return null;
		}
	}

	public static long dateToLong(Date param)
	{
		long date = param.getTime();
		return date;
	}

	public static long dateToLong(Date date , int day)
	{
		//Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, day);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime();
		// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
		// HH:ss");
		// String dateString = formatter.format(date);
		// System.out.println(dateString);
		return date.getTime();
	}

	/**
	 * Parse a datetime string.
	 * 
	 * @param param
	 *            datetime string, pattern: "yyyy-MM-dd HH:mm:ss".
	 * @return java.util.Date
	 */
	public static Date parse(String param)
	{
		Date date = null;
		if (param == null)
		{
			return null;
		} else
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try
			{
				date = sdf.parse(param);
			} catch (ParseException ex)
			{
				ex.printStackTrace();
			}
			return date;
		}
	}
	
	public static Date parseStr(String param) {
		Date date = null;
		if(param == null){
			return null;
		}else{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(param);
		} catch (ParseException ex) {
		}
		return date;
		}
	}

	public static void main(String[] args)
	{

		// long lastEditTime = 10101010101l;
		// System.out.println(lastEditTime);
		// System.out.println(DateUtils.longToDate(lastEditTime));
		// System.out.println(DateUtils.dateToLong(new Date()));
		// System.out.println(DateUtils.longToDate(new Long("1215402693041")));
		// System.out.println(dateToLong(0));
		// System.out.println(dateToLong(1));
	}
	
	public static String DateToString(Date date) {
		if(date!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.format(date);	
		}else{
			return "";
		}
		
	}
	
	
	 /**   
	     * 计算两个日期之间相差的月数   
	     * @param date1   
	     * @param date2   
	     * @return   
	     */    
	    public static int getMonths(Date date1, Date date2){     
	        int iMonth = 0;     
	        int flag = 0;     
	        try{     
	            Calendar objCalendarDate1 = Calendar.getInstance();     
	            objCalendarDate1.setTime(date1);     
	     
	            Calendar objCalendarDate2 = Calendar.getInstance();     
	            objCalendarDate2.setTime(date2);     
	     
	            if (objCalendarDate2.equals(objCalendarDate1))     
	                return 0;     
	            if (objCalendarDate1.after(objCalendarDate2)){     
	                Calendar temp = objCalendarDate1;     
	                objCalendarDate1 = objCalendarDate2;     
	                objCalendarDate2 = temp;     
	            }     
	            if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1.get(Calendar.DAY_OF_MONTH))     
	                flag = 1;     
	     
	            if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR))     
	                iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR))     
	                        * 12 + objCalendarDate2.get(Calendar.MONTH) - flag)     
	                        - objCalendarDate1.get(Calendar.MONTH);     
	            else    
	                iMonth = objCalendarDate2.get(Calendar.MONTH)     
	                        - objCalendarDate1.get(Calendar.MONTH) - flag;     
	     
	        } catch (Exception e){     
	         e.printStackTrace();     
	        }     
	        return iMonth;     
	    }  
	    public static int getYears(Date date1, Date date2){
	    	if(date1!=null&&date2!=null){
	    		return date1.getYear()-date2.getYear();
	    	}else{
	    		return 0;
	    	}
	    	
	    }
}
